package com.amigo.online.provider.user.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.param.result.UserInfo;
import com.amigo.online.provider.user.common.GlobalController;
import com.amigo.online.provider.user.exception.UserException;
import com.amigo.online.provider.user.service.FollowService;


/**
 * 关注相关
 *
 * @author amigo
 */
@RestController
@RequestMapping(value = "/follow", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class FollowController extends BaseController {

    @Resource
    private FollowService followService;

    /**
     * 关注用户
     *
     * @param userId
     * @return
     */
    @GetMapping(value = "/add/{userId}")
    public ResponseEntity<Response> concern(@PathVariable Long userId) {
        if (userId == null || userId <= 0) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        return success(followService.concern(userId));
    }

    /**
     * 取消关注
     *
     * @param userId
     * @return
     */
    @DeleteMapping(value = "/cancel/{userId}")
    public ResponseEntity<Response> cancel(@PathVariable Long userId) {
        if (userId == null || userId <= 0) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        return success(followService.cancel(userId));
    }

    /**
     * 获取我的粉丝列表
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/my/fans/{page}/{size}")
    public ResponseEntity<Response> getMyFans(@PathVariable Integer page, @PathVariable Integer size) {
        page = GlobalController.checkPage(page, size);
        UserInfo u = GlobalController.getNowUser();
        return success(followService.getFansById(u.getId(), page, size));
    }

    /**
     * 获取我关注的人的列表
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/my/concern/{page}/{size}")
    public ResponseEntity<Response> getMyConcern(@PathVariable Integer page, @PathVariable Integer size) {
        UserInfo u = GlobalController.getNowUser();
        page = GlobalController.checkPage(page, size);
        return success(followService.getConcernById(u.getId(), page, size));
    }

    /**
     * 根据id获得粉丝列表
     *
     * @param id
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/fans/{id}/{page}/{size}")
    public ResponseEntity<Response> getFans(@PathVariable Long id, @PathVariable Integer page, @PathVariable Integer size) {
        page = GlobalController.checkPage(page, size);
        return success(followService.getFansById(id, page, size));
    }

    /**
     * 获取我关注的人的视频
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/my/concern/video/{page}/{size}")
    public ResponseEntity<Response> getMyConcernVideos(@PathVariable Integer page, @PathVariable Integer size) {
        page = GlobalController.checkPage(page, size);
        return success(followService.getMyConcernVideos(page, size));
    }

    /**
     * 根据id获得粉丝列表
     *
     * @param id
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/concern/{id}/{page}/{size}")
    public ResponseEntity<Response> getConcern(@PathVariable Long id, @PathVariable Integer page, @PathVariable Integer size) {
        page = GlobalController.checkPage(page, size);
        return success(followService.getConcernById(id, page, size));
    }

    /**
     * 获取好友列表
     *
     * @return
     */
    @GetMapping(value = "/my/getFriends")
    public ResponseEntity<Response> getFriends() {
        return success(followService.getFriends());
    }

    /**
     * 获取我和这些人的关注状态
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/my/connect/user")
    public ResponseEntity<Response> findFollowStateByUserIdAndUserIds(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "ids", required = false) List<Long> ids) {
        return success(followService.findFollowStateByUserIdAndUserIds(id, ids));
    }
}
